package com.breaktian.demoapp.ui;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.breaktian.assemble.utils.LogUtil;
import com.breaktian.assemble.webview.ProgressWebView;
import com.breaktian.demoapp.R;
import com.breaktian.demoapp.provider.BaseProvider;
import com.breaktian.demoapp.toobar.ToolBarBuilder;
import com.breaktian.demoapp.ui.base.BaseActivity;

public class WebviewActivity extends BaseActivity implements View.OnClickListener {

    private Toolbar toolbar;
    private ProgressWebView progressWebView;

    public static final String URL = "url";
    public static final String TITLE = "title";
    private String mUrl;
    private String mTitle;
    private TextView tv_webview_close;
    private ImageView iv_webview_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);

        mUrl = getIntent().getStringExtra(URL);
        mTitle = getIntent().getStringExtra(TITLE);
        initView();




    }

    @Override
    protected BaseProvider getProvider() {
        return null;
    }

    private void initView() {
        toolbar = (Toolbar)findViewById(R.id.toolBar);
        new ToolBarBuilder().setToolbar(toolbar).setTitle(mTitle).hideBackIcon(true)
                .build(this);

        progressWebView = (ProgressWebView)findViewById(R.id.progressWebView);
        progressWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                LogUtil.d("shouldOverrideUrlLoading url:"+url);
                tv_webview_close.setVisibility(View.VISIBLE);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        progressWebView.loadUrl(mUrl);

        iv_webview_back = (ImageView)findViewById(R.id.iv_webview_back);
        iv_webview_back.setOnClickListener(this);
        tv_webview_close = (TextView)findViewById(R.id.tv_webview_close);
        tv_webview_close.setOnClickListener(this);
        tv_webview_close.setVisibility(View.GONE);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_webview_back:
                clickBack();
                break;
            case R.id.tv_webview_close:
                clickClose();
                break;
        }
    }

    private void clickClose() {
        finish();
    }

    private void clickBack() {
        if(progressWebView.canGoBack()){
            progressWebView.goBack();
            if(!progressWebView.canGoBack()){
                tv_webview_close.setVisibility(View.GONE);
            }
        }else{
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        clickBack();
//        super.onBackPressed();
    }
}
